package com.nuoniu.sibanyun.service.crm.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nuoniu.sibanyun.entity.crm.CrmQuotedPrice;
import com.nuoniu.sibanyun.entity.crm.CrmQuotedPriceDetails;
import com.nuoniu.sibanyun.mapper.crm.CrmQuotedPriceMapper;
import com.nuoniu.sibanyun.service.crm.ICrmQuotedPriceDetailsService;
import com.nuoniu.sibanyun.service.crm.ICrmQuotedPriceService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 报价单 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-22
 */
@Service
public class CrmQuotedPriceServiceImpl extends ServiceImpl<CrmQuotedPriceMapper, CrmQuotedPrice> implements ICrmQuotedPriceService {

    @Autowired
    private ICrmQuotedPriceDetailsService quotedPriceDetailsService;
    @Autowired
    private CrmQuotedPriceMapper priceMapper;

    @Override
    public Page<CrmQuotedPrice> pageQuotedPrice(Page page, CrmQuotedPrice price) {
        Page<CrmQuotedPrice> pagePrice = priceMapper.pageQuotedPrice(page, price);
        return pagePrice;
    }

    @Override
    public void saveQuotedPrice(CrmQuotedPrice price, List<CrmQuotedPriceDetails> priceDetails) {
        UserInfo userInfo = UserThreadLocal.get();
        if(price == null) {
            price.setCreateUserId(userInfo.getUserId());
            price.setCreateUserName(userInfo.getRealName());
            save(price);
            for(CrmQuotedPriceDetails priceDetail : priceDetails) {
                priceDetail.setQuotedPriceId(price.getId().intValue());
                priceDetail.setCreateUserId(userInfo.getUserId());
                priceDetail.setCreateUserName(userInfo.getRealName());
            }
            quotedPriceDetailsService.saveBatch(priceDetails);
        }
    }
}
